package top.hkyzf.study.datastructure.queue;

import java.util.Scanner;

/**
 * 数组模拟队列（一次性）
 * @author 朱峰
 * @date 2020-12-18 9:34
 */
class ArrayQueue {
    private int head;
    private int tail;
    private int maxSize;
    private int [] array;

    public ArrayQueue(int maxSize) {
        this.head = -1;
        this.tail = -1;
        this.maxSize = maxSize;
        this.array = new int[maxSize];
    }

    // 判断队列是否满
    public boolean isFull() {
        return tail == maxSize-1;
    }

    // 队列是否为空
    public boolean isEmpty() {
        return head == tail;
    }

    public void addQueue(int i) {
        // 判断是否已满
        if(isFull()) {
            throw new RuntimeException("队列已满，无法添加");
        }
        tail++;
        array[tail] = i;
    }

    public int getQueue() {
        // 判断是否是空的
        if(isEmpty()) {
            throw new RuntimeException("队列是空的，无法获取");
        }
        head++;
        return array[head];
    }

    public void listQueue() {
        // 判断是否是空的
        if(isEmpty()) {
            throw new RuntimeException("队列是空的");
        }
        // head = 2; tail = 4; temp =3
        int temp = head;
        temp++;
        for(int i=temp; i<=tail; i++) {
            System.out.printf("a[%d]=%d\n", i-temp, array[i]);
        }
    }

    public int showHead() {
        // 判断是否是空的
        if(isEmpty()) {
            throw new RuntimeException("队列是空的，没有头元素");
        }
        return array[head+1];
    }

}
